//
//  MentalWellbeing.swift
//  HIextractLicve
//
//  Created by HIextractLicve on 2024/12/12.
//

import UIKit
import AVFoundation
import ProgressHUD
class MentalWellbeing: UIViewController {
    var mindfulness:Set<Int>?
    var suggestion:Set<String>?
    var  professionals:Set<CGFloat>?
    var exchangeHTLC:[String:Array<Dictionary<String,String>>]?
    var eachVioViewHTLC:THLCNOrBUTTOn?
    var mindfulnessPageHTL:THLCNPageControll?
    var journeyHTLC:UIView?
    var  passonHtlacData:Dictionary<String,String>
    init(stressdescHTL:Set<Int>,passonHtlacData: Dictionary<String, String>) {
        self.passonHtlacData = passonHtlacData
        super.init(nibName: nil, bundle: nil)
        mindfulness = [99]
        suggestion = ["decompression"]
        professionals = [12.1]
        exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private var  codeingcamreraSessionHTLC = AVCaptureSession()
    private var posionCamreaHTLC: AVCaptureDevice.Position = .front
    
    
    
    @IBOutlet weak var comabuNameLBlHTLC: UILabel!
    
    @IBOutlet weak var bighisImageView: UIImageView!
    
    @IBOutlet weak var layerbGHTLVC: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(diBakissBoockHTLC), name: NSNotification.Name("htldeleteuser"), object: nil)
       
        layerbGHTLVC.layer.cornerRadius = 23
        layerbGHTLVC.layer.masksToBounds = true
        
        comabuNameLBlHTLC.text = passonHtlacData["HTLCkiName"]
        bighisImageView.image = UIImage(named: passonHtlacData["HTLCAvater"] ?? "")
        layerbGHTLVC.image = HTLCAllDealRelaxing.Relaxing.showingHTLimgHead
        
        var huihuo = 43
        huihuo += 1
        var pagenorHTLC = 23
        pagenorHTLC -= 2
        let community:Set<Int> = [huihuo,pagenorHTLC]
        AVAudioSession.sharedInstance().requestRecordPermission { (allowed) in
            
        }
        AVCaptureDevice.requestAccess(for: .video) { granted in
           
        }
        if self.navigationController?.navigationBar.isHidden == true {
            journeyHTLC?.alpha = 1
            journeyHTLC?.layer.cornerRadius = 12
        }
        journeyHTLC?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)

        eachVioViewHTLC = THLCNOrBUTTOn.init()
        eachVioViewHTLC?.setTitle("anchors", for: .normal)
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(eachVioViewHTLC ?? THLCNOrBUTTOn.init())
            view.addSubview(journeyHTLC!)
        }

        mindfulnessPageHTL = THLCNPageControll()
        mindfulnessPageHTL?.numberOfPages = 1
        mindfulnessPageHTL?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.tintColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.sizeToFit()

        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }
        
        guard let cameraHTLC = cahngeCamretPOsitionHTLC(stressdescHTL: community, ocaWeozjoHTLC: posionCamreaHTLC).0,
              let cameraInputHTLC = try? AVCaptureDeviceInput(device: cameraHTLC) else { return }
        
        if codeingcamreraSessionHTLC.canAddInput(cameraInputHTLC) {
            codeingcamreraSessionHTLC.addInput(cameraInputHTLC)
        }
        
        // 设置输出
        let videoOutputHTLC = AVCaptureVideoDataOutput()
        if codeingcamreraSessionHTLC.canAddOutput(videoOutputHTLC) {
            codeingcamreraSessionHTLC.addOutput(videoOutputHTLC)
        }
        
        let mylashingLayerHTLC = AVCaptureVideoPreviewLayer(session: codeingcamreraSessionHTLC)
        mylashingLayerHTLC.frame = self.view.frame
         
         mylashingLayerHTLC.videoGravity = .resizeAspectFill
        self.layerbGHTLVC.layer.addSublayer(mylashingLayerHTLC)
        DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
            self.codeingcamreraSessionHTLC.startRunning()
           
        }
        
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 6, execute: DispatchWorkItem(block: {
            self.codeingcamreraSessionHTLC.stopRunning()
            ProgressHUD.banner(HTLROOOMMeasg.BinrwetioBase64("U29ycnksIHlvdXIgY2FsbCB3YXMgaHVuZyB1cCE="),"", delay: 2.0)
            self.navigationController?.popViewController(animated: true)
        }))
    }

    @IBAction func abounttrUSClickHTLC(_ sender: Any) {
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }
        self.navigationController?.popViewController(animated: true)
    }
    @objc func diBakissBoockHTLC() {
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }
        codeingcamreraSessionHTLC.stopRunning()
       self.navigationController?.popViewController(animated: true)
    }

    @IBAction func declinhungupHTLC(_ sender: Any) {
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }
        
        
            self.navigationController?.popViewController(animated: true)
        
       
        codeingcamreraSessionHTLC.stopRunning()
        
    }
    @IBAction func startRecoreViewoHTLC(_ sender: UIButton) {
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }
        
        
      
            ProgressHUD.banner(nil, HTLROOOMMeasg.BinrwetioBase64("U29ycnksIHBsZWFzZSB1c2UgdGhlIHJlY29yZGluZyBmdW5jdGlvbiBhZnRlciBjb25uZWN0aW5n"), delay: 2.0)
        
        
    }
    
    
    @IBAction func startSwiftchViewoHTLC(_ sender: Any) {
        
        
        codeingcamreraSessionHTLC.beginConfiguration()
      
       
        if posionCamreaHTLC == .back  {
            posionCamreaHTLC = .front
        } else {
            posionCamreaHTLC = .back
        }
        
        // 移除当前输入
        for input in codeingcamreraSessionHTLC.inputs {
            codeingcamreraSessionHTLC.removeInput(input)
            
        }
        
      
        guard let camera = cahngeCamretPOsitionHTLC(stressdescHTL: [334,333], ocaWeozjoHTLC:posionCamreaHTLC).0,
            let cameraInput = try? AVCaptureDeviceInput(device: camera) else { return }
        if codeingcamreraSessionHTLC.canAddInput(cameraInput) {
            codeingcamreraSessionHTLC.addInput(cameraInput)
        }
        
        codeingcamreraSessionHTLC.commitConfiguration()
    }
    
    @IBAction func moreOPrationNOweHTLC(_ sender: Any) {
        
        let Asvc = UIAlertController(title: HTLROOOMMeasg.BinrwetioBase64("UmVwb3J0aW5nIG9yIEJsb2Nr"), message: HTLROOOMMeasg.BinrwetioBase64("QXJlIHlvdSBzdXJlIHlvdSB3YW50IHRvIGJsb2NrIG9yIGJsYWNrbGlzdCB0aGlzIHVzZXI/IFNlbGVjdCB0aGUgb3BlcmF0aW9uIHlvdSB3YW50IHRvIGRvLiBBZnRlciB0aGUgdXNlciBpcyBibGFja2xpc3RlZCwgYWxsIHJlbGF0ZWQgaW5mb3JtYXRpb24gd2lsbCBubyBsb25nZXIgYmUgZGlzcGxheWVk"), preferredStyle:.actionSheet)
        
 
        let ActdeleBlock = UIAlertAction(title: HTLROOOMMeasg.BinrwetioBase64("U3VyZQ=="), style: .default) { acvf in
            ProgressHUD.animate()
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                ProgressHUD.succeed(HTLROOOMMeasg.BinrwetioBase64("VGhlIHVzZXIgaGFzIGJlZW4gYmxhY2tsaXN0ZWQhISE="), delay: 2.5)
               
                HTLCAllDealRelaxing.Relaxing.removeApplerlUserFrom(stressdescHTL: [444,5555,444], WhoINfoHTL:self.passonHtlacData)
            }
           
        }
        
        let reportdeleBlock = UIAlertAction(title: HTLROOOMMeasg.BinrwetioBase64("UmVwb3J0"), style: .default) { acvf in
            self.navigationController?.pushViewController(UntentionSetting.init(), animated: true)
        }
        Asvc.addAction(ActdeleBlock)
       
            Asvc.addAction(reportdeleBlock)
        
       
        self.present(Asvc, animated: true)
    }
    
    @IBAction func startVoiceViewoHTLC(_ sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
       
    }
    
    
    func cahngeCamretPOsitionHTLC(stressdescHTL:Set<Int>,ocaWeozjoHTLC: AVCaptureDevice.Position) -> (AVCaptureDevice?,Int?,Int?) {
       
        let devices = AVCaptureDevice.DiscoverySession(
            deviceTypes: [.builtInWideAngleCamera],
            mediaType: .video,
            position: .unspecified).devices
        
        return (devices.first(where: { $0.position == ocaWeozjoHTLC }),666,666)
       
    }
}
